unit SoundManager;

interface

uses
  Microsoft.DirectX.AudioVideoPlayback;

type
  TSoundManager = class
  private
    { Private Declarations }

    aBackground : Audio;
    aBlockMove : Audio;
    aFinishMap : Audio;
    aGameOver : Audio;
    aCredits : Audio;

    function _isBackgroundPlaying() : Boolean;
    procedure SoundStopped(sender : TObject; e : EventArgs);
  public
    property BackgroundPlaying : Boolean read _isBackgroundPlaying;
    constructor Create;

    procedure PlayBlockMove();
    procedure PlayBackground();
    procedure StopBackground();

    procedure PlayCredits();
    procedure StopCredits();
    
    procedure PlayGameOver();
  end;

implementation

constructor TSoundManager.Create;
begin
  inherited Create;
  // TODO: Add any constructor code here

  aBackground := Audio.Create('media\theme.mp3');
  aBlockMove := Audio.Create('media\button-10.wav');
  aGameOver := Audio.Create('media\gameover.wav');
  aCredits := Audio.Create('media\credits.mid');

  aBlockMove.Volume := -100;

  Include(aBackground.Ending, self.SoundStopped);
end;

procedure TSoundManager.SoundStopped(sender: TObject; e: EventArgs);
begin
  (sender as Audio).Stop();
  (sender as Audio).Play();
end;

function TSoundManager._isBackgroundPlaying() : Boolean;
begin
  result := self.aBackground.Playing;
end;

procedure TSoundManager.PlayBackground();
begin
  self.aBackground.Play();
end;

procedure TSoundManager.PlayCredits();
begin
  self.aCredits.Stop();
  self.aCredits.Play();
end;

procedure TSoundManager.StopCredits();
begin
  self.aCredits.Stop();
end;

procedure TSoundManager.PlayGameOver();
begin
  self.aGameOver.Stop();
  self.aGameOver.Play();
end;

procedure TSoundManager.StopBackground();
begin
  self.aBackground.Pause();
end;

procedure TSoundManager.PlayBlockMove();
begin
  aBlockMove.Stop();
  aBlockMove.Play();
end;

end.
